package com.le.buddha.websocket;

import cn.hutool.core.lang.Assert;
import com.le.buddha.mobile.ConnectRecord;
import com.le.buddha.mobile.User;
import com.le.buddha.mobile.enums.ConnectRecordEnum;
import com.le.buddha.service.IConnectRecordService;
import com.le.buddha.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
@Slf4j
public class CloseHandle {

    @Resource
    private IUserService userService;
    @Resource
    private IConnectRecordService connectRecordService;
    public void handle(String oid) {
        User user = userService.queryUserByToken(oid);
        Assert.notNull(user, "获取不到用户");
        Date now = new Date();
        user.setConnectStatus(2);
        user.setOfflineTime(now);
        userService.updateById(user);
        ConnectRecord connectRecord = new ConnectRecord();
        connectRecord.setCreateDate(now);
        connectRecord.setType(ConnectRecordEnum.下线.getCode());
        connectRecord.setUserId(user.getId());
        connectRecordService.save(connectRecord);
    }
}